package com.webserver.http;

import com.webserver.core.ClientHandler;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Administrator
 * @Description
 * @date 2022/5/17 10:08
 **/
public class HttpServletResponse {
    private Socket socket;

    private int statesCode = 200;
    private String statesReason = "OK";
    //响应头相关信息
    private Map<String, String> headers = new HashMap<>();
    //响应正文
    private File contentFile;


    public HttpServletResponse(Socket socket) {
        this.socket = socket;
    }
    //将当前响应内容以标准的形式发送给客户端
    public void response() throws IOException {
        //发送状态行
        sendStatesLine();
        //发送响应头
        sendHeaders();
        //发送响应正文
        sendContent();

//        System.out.println("响应发送完毕!!!!!!!!!!!!!!!!!!!");
    }

    private void sendStatesLine() throws IOException {
        //发送状态行

        println("HTTP/1.1 " + statesCode + " " + statesReason);
    }
    private void sendHeaders() throws IOException {
        //发送响应头

//        println("Content-Type: text/html");
//        println("Content-Length: " + contentFile.length());
//        for (String key :
//                headers.keySet()) {
//            println(key + ": " + headers.get(key));
//        }
        Set<Map.Entry<String, String>> entrySet = headers.entrySet();
        for (Map.Entry<String, String> entry : entrySet) {
            println(entry.getKey() + ": " + entry.getValue());
        }

        println("");
    }
    private void sendContent() throws IOException {
        //发送响应正文
        if (contentFile != null) {
            try (
                    FileInputStream fis = new FileInputStream(contentFile);
            ) {
                OutputStream out = socket.getOutputStream();
                byte[] data = new byte[1024 * 10];
                int len;
                while ((len = fis.read(data)) != -1) {
                    out.write(data, 0, len);
                }
            }
        }
    }


    public void addHeader(String name, String value){
        headers.put(name, value);
    }

    private void println(String line) throws IOException {
        OutputStream out = socket.getOutputStream();
        byte[] data = line.getBytes(StandardCharsets.ISO_8859_1);
        out.write(data);
        out.write(13);
        out.write(10);
    }

    public int getStatesCode() {
        return statesCode;
    }

    public void setStatesCode(int statesCode) {
        this.statesCode = statesCode;
    }

    public String getStatesReason() {
        return statesReason;
    }

    public void setStatesReason(String statesReason) {
        this.statesReason = statesReason;
    }

    public File getContentFile() {

        return contentFile;
    }

    public void setContentFile(File contentFile) {
        this.contentFile = contentFile;
//        String contentType = null;
//        try {
//            contentType = Files.probeContentType(contentFile.toPath());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        if (contentType != null) {
//            addHeader("Content-Type", contentType);
//        }
        try {
            addHeader("Content-Type", Files.probeContentType(contentFile.toPath()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        addHeader("Content-Length", contentFile.length() + "");
    }
}
